#ifndef YHAIDA_TIMESTAMP_H
#define YHAIDA_TIMESTAMP_H
#include <stdint.h>
#include <iostream>
#include <string>
namespace yhaida
{
    class Timestamp
    {
    public:
        Timestamp();
        ~Timestamp() = default;
        explicit Timestamp(int64_t microSecondsSinceEpoch);

        void swap(Timestamp &other)
        {
            std::swap(_microSecondsSinceEpoch, other._microSecondsSinceEpoch);
        }
        std::string toString() const;
        std::string toFormattedString() const;
        bool valid() const
        {
            return _microSecondsSinceEpoch > 0;
        }
        int64_t microSecondsSinceEpoch() const
        {
            return _microSecondsSinceEpoch;
        }

        static Timestamp now();

        static Timestamp invalid();
        //1s(秒)=10^6μs(微秒)
        static const int kMicroSecondsPerSecond = 1000 * 1000;

    private:
        //微妙
        int64_t _microSecondsSinceEpoch;
    };

    inline bool operator<(Timestamp l, Timestamp r)
    {
        return l.microSecondsSinceEpoch() < r.microSecondsSinceEpoch();
    }
    inline bool operator==(Timestamp l, Timestamp r)
    {
        return l.microSecondsSinceEpoch() == r.microSecondsSinceEpoch();
    }
    /*
    获取两个时间戳的时间差，结果为秒。
    */
    inline double timeDifference(Timestamp high, Timestamp low)
    {
        int64_t diff = high.microSecondsSinceEpoch() - low.microSecondsSinceEpoch();
        return static_cast<double>(diff) / Timestamp::kMicroSecondsPerSecond;
    }

    inline Timestamp addTime(Timestamp timestamp, double seconds)
    {
        int64_t delta = static_cast<int64_t>(seconds * Timestamp::kMicroSecondsPerSecond);
        return Timestamp(timestamp.microSecondsSinceEpoch() + delta);
    }

}

#endif